<?php
    class WZ_Session
    {
        static $_instance = array();
        static $_namespace;
        static $_session;
        
        // singleton
		public static function getInstance($namespace) 
		{
            if (!array_key_exists($namespace, self::$_instance)){
                self::$_instance[$namespace]  = new self;
            }

            self::$_namespace = $namespace;
            self::init();
            return self::$_session;
		}
        
        
        public static function init()
        {
            if (null === $_SESSION[self::$_namespace]) {
                $_SESSION[self::$_namespace] = array();
                $session = new \WZ_SessionObject;
                $session->setNamespace(self::$_namespace);
                self::$_session = $session;
            } else {
                $sessionObject = new \WZ_SessionObject;
                $sessionObject->setNamespace(self::$_namespace);
                foreach ($_SESSION[self::$_namespace] as $key => $value) {
                    $setter = 'set' . ucfirst(\WZ_Inflector::camelize($key));
                    $sessionObject->$setter($value);
                }
                self::$_session = $sessionObject;
            }
            return self::$_session;
        }
        
        public static function destroy()
        {
            $_SESSION[self::$_namespace] = array();
            return self::init();
        }
        
        public static function factory($namespace)
        {
            return self::getInstance($namespace);
        }
        
        public static function zend($namespace)
        {
            return new \Zend_Session_Namespace($namespace);
        }
    }
    